<?php

namespace app\admin\controller;

use app\admin\validate\CheckArticle;
use Curl\Curl;
use think\exception\ValidateException;
use think\facade\Db;

class Server extends Base
{
    protected $mod;

    protected function initialize()
    {
        parent::initialize();
        $this->mod = new \app\admin\model\Server();
    }


    public function index(String $keyword = '', int $limit=10,int $page=1)
    {
        $map = [];
        if ($keyword) {
            $map[] = ['title', 'like', "%$keyword%"];
        }
        $dataList = $this->mod::where($map)->append(['recommends_name','admin_id_name'])->order('id', 'desc')->paginate($limit, false, ['page' => $page, 'query' => ['keyword' => $keyword]]);
        return $this->success('获取成功', $dataList);
    }

    /**
     * 跟新企业服务器信息
     * @return void
     */
    public function update()
    {
        if(request()->isPost()) {
            $params = input();
            $server_data = $this->mod::where('company_id', $params['company_id'])->find();
            if(empty($server_data)){
               return $this->error('服务器信息不存在!');
            }
            $company = Db::name('company')->where('id',$params['company_id'])->find();
            $message = '服务器到期';
            $requers_data = [
                'status'=> $company['status'],
                'end_time'=>$company['endtime'],
                'message'=>$message
            ];
            $url = $server_data['host'].'/server/update';
            $curl = new Curl();
            $curl->post($url, $requers_data);
            if($curl->error){
                return $this->error($curl->error_code);
            }
            $res = $curl->response;
            $curl->close();
            return $res;
        }
    }
}
    